<?php
$statuses = Application_Model_DAO_Payment::getTranslatetPaymentStatuses();
$cols = array(
    'offer' => 'Oferty',
    'catalog' => 'Wpisy do katalogu',
    'adv' => 'Reklamy',
        )
?>

<form action="<?= $this->url(array('controller' => 'payment', 'action' => 'stats'), 'admin', true) ?>" class="form-inline data-range-chooser">
    Okres statystyki:
    <label for="from">od</label>
    <input type="text" id="from" name="from" value="<?= $this->dateStart ?>" />
    <label for="to">do</label>
    <input type="text" id="to" name="to" value="<?= $this->dateEnd ?>" />
    <input type="submit" class="btn" value="Pokaż" />
</form>

<div class="table table-striped table-hover data-table">
    <table class="table-bordered">
        <thead>
            <tr>
                <th rowspan="2">Status płatności</th>
                <?php foreach ($this->stats['source_names'] as $col): ?>
                    <th colspan="2"><?= $cols[$col] ?></th>
                <?php endforeach; ?>
                <th colspan="2">Razem</th>
            </tr>
            <tr>
                <?php foreach ($this->stats['source_names'] as $col): ?>
                    <th>Ilość</th>
                    <th>Kwota [PLN]</th>
                <?php endforeach; ?>
                <th>Ilość</th>
                <th>Kwota [PLN]</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($this->stats['rows'] as $status => $val): ?>
                <tr>
                    <td><?= $statuses[$status] ?></td>
                    <?php foreach ($this->stats['source_names'] as $col): ?>
                        <?php if (isset($val[$col])): ?>
                            <td><?= $val[$col]['cnt'] ?></td>
                            <td><?= sprintf('%.2f', $val[$col]['amount']) ?></td>
                        <?php else: ?>
                            <td></td>
                            <td></td>
                        <?php endif ?>
                    <?php endforeach; ?>
                    <?php if ($this->stats['sum'][$status]['cnt'] != 0): ?>
                        <td><?= $this->stats['sum'][$status]['cnt'] ?></td>
                        <td><?= sprintf('%.2f', $this->stats['sum'][$status]['amount']) ?></td>
                    <?php else: ?>
                        <td></td>
                        <td></td>
                    <?php endif ?>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>
<style type="text/css">
    .data-range-chooser #from,
    .data-range-chooser #to {
        width: 75px;
    }
</style>
<script type="text/javascript">
    $(function() {
        $(".data-range-chooser #from").datepicker({
            defaultDate: "-1w",
            changeMonth: true,
            numberOfMonths: 2,
            dateFormat: "yy-mm-dd",
            maxDate: '<?= $this->dateEnd ?>',
            onClose: function(selectedDate) {
                console.log(selectedDate);
                $(".data-range-chooser #to").datepicker("option", "minDate", selectedDate);
            }
        });
        $(".data-range-chooser #to").datepicker({
            defaultDate: "-1w",
            changeMonth: true,
            numberOfMonths: 2,
            dateFormat: "yy-mm-dd",
            minDate: '<?= $this->dateStart ?>',
            onClose: function(selectedDate) {
                $(".data-range-chooser #from").datepicker("option", "maxDate", selectedDate);
            }
        });
    });
</script>
